﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using MyGIS;
class ApiClient
    {
    private static readonly HttpClient client = new HttpClient();

    public static async Task<List<AirStation>> GetStationsAsync(string url, string token)
    {
        client.DefaultRequestHeaders.Clear();
        client.DefaultRequestHeaders.Add("X-APISpace-Token", token);

        var response = await client.GetAsync(url);
        if (!response.IsSuccessStatusCode)
        {
            throw new Exception($"请求失败，状态码：{response.StatusCode}");
        }

        string json = await response.Content.ReadAsStringAsync();
       
        var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(json);

        if (apiResponse == null || apiResponse.status != 0 || apiResponse.result?.realtimeAqi?.stations == null)
        {
            throw new Exception("接口返回异常或无数据");
        }

        return apiResponse.result.realtimeAqi.stations;
    }

}

